<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="字段名称" prop="columnName">
        <el-input
          v-model="queryParams.columnName"
          placeholder="请输入字段名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_common_status"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['reportworld:tableDetailStandardCheck:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['reportworld:tableDetailStandardCheck:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['reportworld:tableDetailStandardCheck:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['reportworld:tableDetailStandardCheck:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border="border" v-loading="loading" max-height="550px"
              :data="tableDetailStandardCheckList"
              @selection-change="handleSelectionChange"    @row-click="rowClick"    ref="tableDetailStandardCheck"
              @sort-change="handleSortChange" :row-style="tableRowStyle">
      <el-table-column type="selection" width="55" align="center" />
        <el-table-column  v-hasPermi="['reportworld:tableDetailStandardCheck:query']"
          label="操作" align="center" width="100" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleUpdate(scope.row,false)"
                v-hasPermi="['reportworld:tableDetailStandardCheck:query']"
              >详情</el-button>
            </template>
        </el-table-column>
      <el-table-column label="字段名称" align="center" width="150" prop="columnName" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="字段中文名称" align="center" width="200" prop="columnChineseName" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="字段类型" width="150" align="center" prop="columnType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.column_type" :value="scope.row.columnType"/>
        </template>
      </el-table-column>
      <el-table-column label="字段长度" width="150" align="center" prop="columnLength" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="小数位数" width="150" align="center" prop="columnDecimal" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="是否可为空" width="180" align="center" prop="columnNull">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.columnNull"/>
        </template>
      </el-table-column>
      <el-table-column label="默认值"  width="180" align="center" prop="columnDefault" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="是否主键" width="150" align="center" prop="primaryKey">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.primaryKey"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="createTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加人" align="center" width="150" prop="createBy" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="编辑时间" align="center" prop="updateTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="编辑人" align="center" width="150" prop="updateBy" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改字段校验规则对话框 -->
    <el-dialog v-if="open"   :title="title" :close-on-click-modal ="false" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="字段名称" prop="columnName">
              <el-input clearable v-model="form.columnName" placeholder="请输入字段名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="字段中文名称" prop="columnChineseName">
              <el-input clearable v-model="form.columnChineseName" placeholder="请输入字段中文名称"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="字段类型" prop="columnType">
              <el-select clearable v-model="form.columnType" placeholder="请选择字段类型">
                <el-option
                  v-for="dict in dict.type.column_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="字段长度" prop="columnLength">
              <el-input clearable placeholder="请输入字段长度" v-model="form.columnLength" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="小数位数" prop="columnDecimal">
              <el-input clearable  v-model="form.columnDecimal" placeholder="请输入小数位数"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否可为空" prop="columnNull">
              <el-select clearable v-model="form.columnNull" placeholder="请选是否可为空">
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="默认值" prop="columnDefault">
              <el-input clearable v-model="form.columnDefault"  placeholder="请输入默认值"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否主键" prop="primaryKey">
              <el-select clearable v-model="form.primaryKey" placeholder="请选是否主键">
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_common_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{dict.label}}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input maxlength="500" clearable v-model="form.remark" type="textarea"
                        placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="isUpdate" :loading="butloading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTableDetailStandardCheck, getTableDetailStandardCheck, delTableDetailStandardCheck, addTableDetailStandardCheck, updateTableDetailStandardCheck } from "@/api/reportworld/tableDetailStandardCheck";
import { getTableRowStyle } from "@/utils/yss";
export default {
  name: "TableDetailStandardCheck",
  dicts: ['column_type', 'sys_common_status','sys_yes_no'],
  data() {
    return {
      butloading:false,
      isUpdate:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字段校验规则表格数据
      tableDetailStandardCheckList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        columnName: null,
        columnChineseName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        columnName: [
          { required: true, message: "字段名称不能为空", trigger: "blur" }
        ],
        columnChineseName: [
          { required: true, message: "字段中文名称不能为空", trigger: "blur" }
        ],
         columnType: [
          { required: true, message: "字段类型不能为空", trigger: "change" }
        ]
      },
      selectList:[],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询字段校验规则列表 */
    getList() {
      this.loading = true;
      listTableDetailStandardCheck(this.queryParams).then(response => {
        this.tableDetailStandardCheckList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        columnName: null,
        columnChineseName: null,
        columnType: null,
        columnLength: null,
        columnDecimal: null,
        columnNull: null,
        columnDefault: null,
        primaryKey: null,
        status: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectList = selection;
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
      /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
     },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加字段校验规则";
      this.isUpdate =true;
         this.form.columnType =  parseInt(this.dict.type.column_type[0].value);
         this.form.status =  parseInt(this.dict.type.sys_common_status[0].value);
        //this.form.columnNull = parseInt(this.dict.type.sys_yes_no[0].value);
         //this.form.primaryKey = parseInt(this.dict.type.sys_yes_no[1].value);
    },
    /** 修改按钮操作 */
    handleUpdate(row,isUpdate) {
      this.reset();
      const id = row.id || this.ids
      getTableDetailStandardCheck(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改字段校验规则";
       this.isUpdate =true;
      if(isUpdate == false){
        this.title = "字段校验规则详情";
        this.isUpdate =false;
      }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.butloading=true;
          if (this.form.id != null) {
            updateTableDetailStandardCheck(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {this.butloading=false});
          } else {
            addTableDetailStandardCheck(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList().finally(() => {this.butloading=false});
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.loading=true;
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据项？').then(function() {
        return delTableDetailStandardCheck(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.ids=JSON.stringify(this.ids);
      this.download('reportworld/tableDetailStandardCheck/export', {
        ...this.queryParams
      }, `tableDetailStandardCheck_${new Date().getTime()}.xlsx`)
    },
    tableRowStyle({ row, rowIndex}){
        return getTableRowStyle(row,rowIndex,this.ids);
    },
    rowClick(row, column, event) { //单击行
      this.$refs.tableDetailStandardCheck.toggleRowSelection(row);
    }
  }
};
</script>
